﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TMS
{
    public partial class CreateEditProjectControl : UserControl
    {
        bool edit = false;
        Project project = new Project();
        Users ActiveUser = Program.GetActiveUser;
        Users users = new Users();
        Partner partner = new Partner();

        public CreateEditProjectControl()
        {
            Program.GetMainForm.Text = "TMS - Нов проекти";
            InitializeComponent();
            edit = false;
            FillResponsibels();
            cmbResponsible.SelectedItem = ActiveUser.GetName() + " " + ActiveUser.GetSurname();
            cmbPartners.ValueMember = "name";
            FillPartners();
        }

        public CreateEditProjectControl(DataGridViewRow parameters)
        {
            Program.GetMainForm.Text = "TMS - Проект:" + parameters.Cells[1].Value.ToString(); 
            InitializeComponent();
            edit = true;
            FillResponsibels();
            cmbPartners.ValueMember = "name";
            FillPartners();
            project.ID=Convert.ToInt32(parameters.Cells[0].Value.ToString());
            project.Name = parameters.Cells[1].Value.ToString();
            project.Contributor = parameters.Cells[5].Value.ToString();
            txtProjectName.Text = parameters.Cells[1].Value.ToString();
            dtpProjectStartDate.Value = Convert.ToDateTime(parameters.Cells[2].Value.ToString());
            dtpProjectEndDate.Value = Convert.ToDateTime(parameters.Cells[3].Value.ToString());
            cbProjectStatus.SelectedItem = parameters.Cells[4].Value.ToString();
            cmbPartners.SelectedValue = parameters.Cells[5].Value.ToString();
            cmbResponsible.SelectedItem = parameters.Cells[6].Value.ToString();
            if ((ActiveUser.GetName() + " " + ActiveUser.GetSurname()).ToString() != cmbResponsible.SelectedItem.ToString())
            gbProjectData.Enabled = false;
            
            


        }

              

        private void btnSaveProject_Click(object sender, EventArgs e)
        {
            project.Name = txtProjectName.Text;
            project.Start_Date = dtpProjectStartDate.Value.Date.ToString("yyyy-MM-dd HH:mm:ss");
            project.End_Date = dtpProjectStartDate.Value.Date.ToString("yyyy-MM-dd HH:mm:ss");
            project.Status = cbProjectStatus.SelectedItem.ToString();
            project.Contributor = cmbPartners.SelectedValue.ToString();
            project.Responsible = cmbResponsible.SelectedItem.ToString();

            if (edit == false)
            {
                project.AddtoDB();
            }
            else
            {
                project.UpdateProductInDB();
            }
            this.Dispose();
            Program.GetMainForm.ShowControl(ControlsEnum.PROJECTS_CONTROL);
            
        }

        private void btnDeleteProject_Click(object sender, EventArgs e)
        {
            if (cbProjectStatus.SelectedItem.ToString() == "Барање на понуда")
            {
                project.DeleteProjectFromDB();
                this.Dispose();
                Program.GetMainForm.ShowControl(ControlsEnum.PROJECTS_CONTROL);
            }
            else
            {
                MessageBox.Show("Не е дозволено бришење на проекти кои содржат документи");
            }
        }

        private void btOffers_Click(object sender, EventArgs e)
        {
            
            
            DataGridViewRow parametars = new DataGridViewRow();
            DataGridView dgvtmp = new DataGridView();
            dgvtmp.ColumnCount = 3;
            dgvtmp.RowCount = 1;
            dgvtmp.Rows[0].Cells[0].Value = Convert.ToInt32(project.ID);
            dgvtmp.Rows[0].Cells[1].Value = project.Name;
            dgvtmp.Rows[0].Cells[2].Value = project.Contributor;
           
            parametars = dgvtmp.Rows[0];
            Program.GetMainForm.registerInHistoryMain(ControlsEnum.OFFERS_CONTROL, parametars);
            Program.GetMainForm.ShowControl(ControlsEnum.OFFERS_CONTROL,parametars);
            this.Dispose();

        }

        public void FillResponsibels()
        {
            DataTable table = users.GetAllUserNameAndSurname();
            foreach (DataRow row in table.Rows)
            {
                cmbResponsible.Items.Add(row.ItemArray[0].ToString());
                cmbResponsible.Items.Remove("Администратор ");
                cmbResponsible.SelectedItem = ActiveUser.GetName() + " " + ActiveUser.GetSurname();
            }
        }

        private void btWorkOrders_Click(object sender, EventArgs e)
        {
           
        }

        public void FillPartners()
        {
            DataTable table = partner.GetAllPartners();
            cmbPartners.DataSource = table;
        }

        private void ToStOrderRequest_Click(object sender, EventArgs e)
        {
            this.Dispose();
            Program.GetMainForm.ShowControl(ControlsEnum.CREATE_EDIT_ORDER_REQUEST_CONTROL);
        }

        private void toolStripLabel2_Click(object sender, EventArgs e)
        {
            this.Dispose();

            DataGridViewRow parametars = new DataGridViewRow();
            DataGridView dgvtmp = new DataGridView();
            dgvtmp.ColumnCount = 3;
            dgvtmp.RowCount = 1;
            dgvtmp.Rows[0].Cells[0].Value = Convert.ToInt32(project.ID);
            dgvtmp.Rows[0].Cells[1].Value = project.Name;
            dgvtmp.Rows[0].Cells[2].Value = project.Contributor;

            parametars = dgvtmp.Rows[0];
            Program.GetMainForm.registerInHistoryMain(ControlsEnum.OFFERS_CONTROL, parametars);
            Program.GetMainForm.ShowControl(ControlsEnum.OFFERS_CONTROL, parametars);
        }

        

        private void ToStBack_Click(object sender, EventArgs e)
        {
            if (Program.GetMainForm.Back())
                this.Dispose();
        }


        private void ToStForward_Click(object sender, EventArgs e)
        {
            if (Program.GetMainForm.Forward())
                this.Dispose();
        }

        private void ToStWorkOrders_Click(object sender, EventArgs e)
        {
            this.Dispose();

            DataGridViewRow parametars = new DataGridViewRow();
            DataGridView dgvtmp = new DataGridView();
            dgvtmp.ColumnCount = 3;
            dgvtmp.RowCount = 1;
            dgvtmp.Rows[0].Cells[0].Value = Convert.ToInt32(project.ID);
            dgvtmp.Rows[0].Cells[1].Value = project.Name;
            dgvtmp.Rows[0].Cells[2].Value = project.Contributor;

            parametars = dgvtmp.Rows[0];
            Program.GetMainForm.registerInHistoryMain(ControlsEnum.WORKORDER_CONTROL, parametars);
            Program.GetMainForm.ShowControl(ControlsEnum.WORKORDER_CONTROL, parametars);
        }

        private void ToStAgreements_Click(object sender, EventArgs e)
        {
            //Program.GetMainForm.ShowControl(ControlsEnum.AGREEMENTS_CONTROL);
            Program.GetMainForm.ShowControl(ControlsEnum.CREATE_EDIT_AGREEMENT_CONTROL);
            this.Dispose();
        }

       
    }
}
